class Admin::UsersController < ApplicationController
  
  def index
    @title ="个人首页"
    redirect_to profile_for(User.find(session[:user_id]))
#    render :layout => false
  end
  
  def select_role
    @title = "选择身份"
  end
  
  def register
    @role = params[:role]
    @title = "用户注册-分度"
    unless [DESIGNER,DEMANDER].include?(@role)
      flash[:notice] = "请选择您的注册方向"
      return 
    end
    if @role == DESIGNER
      @scripts = ['jquery.validate.min.js','fendu/fendu_reg_cybao.js']
    else
      @scripts = ['jquery.validate.min.js','fendu/fendu_reg_cyxun.js']
    end
    if param_posted?(:user)
      @user = User.new(params[:user])
      @user.role = @role
      if @user.valid_with_captcha?
        if @user.save
          flash[:notice] = "#{@user.username}注册成功，我们已将验证邮件发送到您的邮箱，清注意查收"
          redirect_to :action => :regist_success, :id => @user
        else
          flash[:notice] = "创建帐号失败"
        end
      else
        flash[:notice] = "验证码不正确"
      end
    end
  end
  
  def regist_success
      @title = "收电子邮件"
      @scripts = ['fendu/fendu_regsuccess.js']
      id = params[:id]
      @user = User.find(id)
      flash[:notice] = "不存在该用户" unless @user
  end 
  
  def activate
    #logout_keeping_session!
    @title = "激活您的帐号-分度"
    @user = User.find_by_activation_code(params[:activation_code]) unless params[:activation_code].blank?
    if (!params[:activation_code].blank?) && @user && !@user.active?
      @user.activate!
      flash[:notice] = "帐号成功激活"
#      @user.login!(session)
    else
      flash[:notice] = "激活码不匹配，或者您的帐号可能已经激活"
    end
  end
  
  def sendmail
    id = params[:id]
    user = User.find_by_username(id)
    if user && !user.active?
      UserMailer.deliver_signup_notification(user)
      render :json => "发送成功"
    else
      render :json => "已激活，或不存在该用户"
    end
  end
  
  def modifyemail
    id = params[:id]
    @success = false
    @user = get_user_by_id(id)
    email = params[:email]
    if @user
      @user.email = email
      if @user.save
        UserMailer.deliver_signup_notification(@user)
        @success = true
      end
    end
    render :layout => false
#    redirect_to :action => :regist_success, :id => id
  end

  def update_captcha
    render :layout => false
  end
  
  def correct_captcha
    captcha = Captcha.find_by_key(params[:simple_captcha_key])
    input_value = params[:simple_captcha].downcase
    value = captcha.value.downcase
    respond_to do |format|
      format.json { render :json => input_value==value}
    end
  end
  
  def user_exist
    if params[:username]
      username =params[:username]
      user= User.find_by_username(username)
    elsif params[:email]
      email =params[:email]
      user= User.find_by_email(email)
    end
    respond_to do |format| 
      format.json { render :json => !user }  
    end
  end
  
  def login #友好的页面转向功能
    @title = "登录-分度"
    if request.get?
      @user = User.new(:remember_me => remember_me_string)
    elsif param_posted?(:user)
        @user = User.new(params[:user])#ensure the info user input still existence
        user = User.authenticate(@user.username,@user.passwd)
        if user
          if user.active?
            user.login!(session)
            @user.remember_me? ? user.remember!(cookies) : user.forget!(cookies)
            flash[:notice] = "#{user.username} 已登录!"
            redirect_to_forwarding_url
          else
            @user.clear_password!
            flash.now[:notice] = "您的帐号未激活，请去#{user.email}激活!"
          end
        else 
          @user.clear_password!
          flash.now[:notice] = "无效的用户名或密码"
        end
    end
  end
  
  def logout
    User.logout!(session,cookies)#warning will modify session
    flash[:notice]||= "退出分度"
#    redirect_to :action => "index", :controller => "site"跳到网站首页
    redirect_to :action => :login
  end
  
  def forgot_password
    @title = "找回密码"
    if param_posted?(:user)
      if @user=User.exist?(params)
        @user.save_pwd_checkcode
        UserMailer.deliver_modify_password(@user)
        flash[:notice] = "我们已经将确认邮件发送到您的邮箱，请注意查收"
      else
        flash[:notice] = "不存在该用户"
      end
    end
  end
  
  def resent_mail
    @user = User.find_by_username(params[:username])
    if @user
      @user.save_pwd_checkcode
      UserMailer.deliver_modify_password(@user)
      render :json => "邮件发送成功"
    else
      render :json => "不存在该用户"
    end
  end

  def reset_password
    if logged_in?
      flash[:notice] = "您已经登陆" 
      return
    end
    if request.get?
      @user = correct_user?(params)
      unless @user
        flash[:notice] = "确认码不正确或您已使用过该链接，请重新申请"
        return
      end
      session[:username] = @user.username
    else
      @user = User.find_by_username(session[:username]) if session[:username]
      unless @user
        flash[:notice] = "不存在该用户" 
        return
      end
      if @user.right_password?(params)
        @user.update_password(params)
        flash[:notice] = "密码修改成功，请登录"
        redirect_to :action => :login
      end
    end
  end
  private
  def remember_me_string
    cookies[:remember_me] || "0"
  end
  
  def redirect_to_forwarding_url
    if redirect_url = session[:protect_page]#赋值，并判断是否为空
       session[:protect_page] = nil
       redirect_to redirect_url
    else
#       redirect_to :action => "index"
        redirect_to :action => :index, :controller => "/user"
    end
  end
  
  def correct_user?(params)
    user = User.find_by_pwd_check_code(params[:check_code]) unless params[:check_code].blank?
    if (!params[:check_code].blank?) && user && !user.checked?
      user.check!
      user
    else
#      flash[:notice]  = "无法找到该用户，请确认您是否已通过该链接修改过密码，你可以通过登录页的忘记密码链接再次修改"
      user = nil
    end
  end
end
